package com.commerceerp.system.controller;

import com.commerceerp.system.entity.Constant;
import com.commerceerp.system.entity.OrderInfo;
import com.commerceerp.system.service.LoginService;
import com.commerceerp.system.service.OrderService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.RdPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(description = "订单接口", value = "订单接口")
@Scope("prototype")
@RestController
@EnableAutoConfiguration
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "订单状态统计（orderStatus 1:待审核,2:待处理,3:待发货,4:发货成功,5:发货失败,6:取消,7:完成）")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/orderCount", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult orderCount(HttpServletRequest request) throws IOException {

        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }

        String userId = to.getData().toString();

        return orderService.orderCount(userId);
    }

    @ApiOperation(value = "获取订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderChannel", value = "平台渠道（1:eaby,2:amazon,3:wish）", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "storeName", value = "店铺账号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressCountry", value = "国家区域", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderStatus", value = "订单状态（1:待审核,2:待处理,3:待发货,4:发货成功,5:发货失败,6:取消,7:完成）", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderDownTimePX", value = "下单时间排序（1:正序,2:反序）", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "orderMoneyPX", value = "订单金额排序（1:正序,l2：反序）", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "orderSkuPX", value = "产品sku（1:正序,l2：反序）", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/getAllOrderInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getAllOrderInfo(
            @RequestParam(value = "orderChannel", required = false) String orderChannel,
            @RequestParam(value = "storeName", required = false) String storeName,
            @RequestParam(value = "orderId", required = false) String orderId,
            @RequestParam(value = "addressCountry", required = false) String addressCountry,
            @RequestParam(value = "orderStatus", required = false) String orderStatus,
            @RequestParam(value = "orderDownTimePX", required = false) Integer orderDownTimePX,
            @RequestParam(value = "orderMoneyPX", required = false) Integer orderMoneyPX,
            @RequestParam(value = "orderSkuPX", required = false) Integer orderSkuPX,
            @RequestParam(value = "beginTime", required = false) String beginTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "current") Integer current,
            @RequestParam(value = "pageSize") Integer pageSize, HttpServletRequest request) throws IOException {

        ApiResult result = null;

        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }

        String userId = to.getData().toString();

        PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));

        List<Map> orderInfoList = orderService.getAllOrderInfo(userId,orderChannel,storeName,orderId,addressCountry,orderStatus,orderDownTimePX,orderSkuPX,orderMoneyPX,beginTime,endTime);

        //获取分页信息
        PageInfo<Map> pageInfo = new PageInfo<>(orderInfoList);
        RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));

        result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"查询成功",orderInfoList,rdPage);

        return result;
    }


    @ApiOperation(value = "更改订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderStatus", value = "订单状态(1:待审核,2:待处理,3:待发货,4:发货成功,5:发货失败)", required = false, dataType = "String")
    })
    @RequestMapping(value = "/updOrderStatus", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updOrderStatus(@RequestParam(value = "orderId", required = false) String orderId,
                                    @RequestParam(value = "orderStatus", required = false) String orderStatus) throws IOException {

        ApiResult result = null;

        Integer success = orderService.updOrderStatus(orderId,orderStatus);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }


    @ApiOperation(value = "查看订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", value = "订单号", required = false, dataType = "String")
    })
    @RequestMapping(value = "/getOrderDetailInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getOrderDetailInfo(@RequestParam(value = "orderId", required = false) String orderId) throws IOException {

        return new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",orderService.getOrderDetailInfo(orderId),null);
    }


    @ApiOperation(value = "修改订单收货信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressRecipients", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressLocationA", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressLocationB", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressCity", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressProvince", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressCountry", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressPostcode", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressPhone", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "addressRemark", value = "订单号", required = false, dataType = "String")

    })
    @RequestMapping(value = "/updOrderAddressInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updOrderAddressInfo(@RequestParam(value = "orderId", required = false) String orderId,
                                         @RequestParam(value = "addressRecipients", required = false) String addressRecipients,
                                         @RequestParam(value = "addressLocationA", required = false) String addressLocationA,
                                         @RequestParam(value = "addressLocationB", required = false) String addressLocationB,
                                         @RequestParam(value = "addressCity", required = false) String addressCity,
                                         @RequestParam(value = "addressProvince", required = false) String addressProvince,
                                         @RequestParam(value = "addressCountry", required = false) String addressCountry,
                                         @RequestParam(value = "addressPostcode", required = false) String addressPostcode,
                                         @RequestParam(value = "addressPhone", required = false) String addressPhone,
                                         @RequestParam(value = "addressRemark", required = false) String addressRemark) throws IOException {

        ApiResult result = null;

        OrderInfo orderInfo = new OrderInfo();

        orderInfo.setOrderId(orderId);
        orderInfo.setAddressRecipients(addressRecipients);
        orderInfo.setAddressLocationA(addressLocationA);
        orderInfo.setAddressLocationB(addressLocationB);
        orderInfo.setAddressCity(addressCity);
        orderInfo.setAddressProvince(addressProvince);
        orderInfo.setAddressCountry(addressCountry);
        orderInfo.setAddressPostcode(addressPostcode);
        orderInfo.setAddressPhone(addressPhone);
        orderInfo.setAddressRemark(addressRemark);

        Integer success = orderService.updOrderAddressInfo(orderInfo);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }

    @ApiOperation(value = "新增订单物流信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "logisticName", value = "物流名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "logisticPhone", value = "物流联系方式", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "logisticInfo", value = "物流信息", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "logisticRemark", value = "物流备注", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "logisticOrderId", value = "订单号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "logisticStatus", value = "物流状态", required = false, dataType = "String"),

    })
    @RequestMapping(value = "/saveLogisticInfoByOrderId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveLogisticInfoByOrderId(@RequestParam(value = "logisticName", required = false) String logisticName,
                                         @RequestParam(value = "logisticPhone", required = false) String logisticPhone,
                                         @RequestParam(value = "logisticInfo", required = false) String logisticInfo,
                                         @RequestParam(value = "logisticRemark", required = false) String logisticRemark,
                                         @RequestParam(value = "logisticOrderId", required = false) String logisticOrderId,
                                         @RequestParam(value = "logisticStatus", required = false) String logisticStatus) throws IOException {

        ApiResult result = null;

        Integer success = orderService.saveLogisticInfoByOrderId(logisticName,logisticPhone,logisticInfo,logisticRemark,logisticOrderId,logisticStatus);

        if(success == 1){
            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
        }else{
            result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
        }

        return result;
    }

}
